package chapter1.zyq2_senFile;

import java.net.InetSocketAddress;
import java.net.Socket;

public class Client5 {
    public static void main(String[] args) {
        Client5 client5 = new Client5();
        client5.communicate();
    }

    private void communicate() {
        InetSocketAddress isAddr=new InetSocketAddress("localhost",8001);
        Socket socket=new Socket();
        try {
            socket.connect(isAddr, 20000);
            System.out.println("客户端连接成功，开始会话...");


            Thread.sleep(900);
            String filePath="D:\\m.png";
            String fileName=filePath.substring(filePath.lastIndexOf("\\")+1);
            System.out.println("发送文件名:"+fileName);
            SendReceiveUtil_File5.sendString(socket,fileName);

            Thread.sleep(900);
            System.out.println("发送文件");
            SendReceiveUtil_File5.sendFile(socket,filePath);


            Thread.sleep(900);
            fileName=SendReceiveUtil_File5.receiveString(socket);
            System.out.println("接收的文件的文件名:"+fileName);

            Thread.sleep(900);
            filePath="D:\\receiveFromServer-"+fileName;
            System.out.println("接收并保存文件: " + filePath);
            SendReceiveUtil_File5.receiveFile(socket,filePath);

            socket.close();
            System.out.println("会话结束，客户端关闭连接。");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
